package com.cnd.scm.datasource;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.cnd.scm.datasource.aop.DataSourceAOP;
import com.cnd.scm.datasource.constant.DataSourceKey;
import com.cnd.scm.datasource.util.DynamicDataSource;
import com.cnd.scm.jdbcprocessor.helper.SqlHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;


/**
 * @version V1.0
 * @ClassName: DataSourceAutoConfig
 * @Description:TODO(数据源配置类)
 * @author: 李启联
 * @date: 2019-12-18 10:35
 * @Copyright: Copyright (c), Xiamen C&D Information Technology co.,Ltd. All rights reserved.
 * 申明：本内容未经作者本人同意,禁止外泄以及用于其他的商业项目.
 */
@Configuration
@AutoConfigureAfter(DruidDataSourceAutoConfigure.class)
@ConditionalOnProperty(name = {"spring.datasource.dynamic.enable"}, matchIfMissing = false, havingValue = "true")
@Import(DataSourceAOP.class)
@Slf4j
public class MultiDataSourceConfig {

    public MultiDataSourceConfig() {
        System.out.println("MultiDataSourceConfig Created...");
        log.info("MultiDataSourceConfig Created ...");
    }

    //	创建数据源
   //	所有引入db-core的模块都需要一个核心库，可以是user-center，也可以是oauth-center,file-center ,sms-center
    /**
     * Core DataSource
     * @Primary 注解用于标识默认使用的 DataSource Bean，因为有三个 DataSource Bean，该注解可用于 Core
     * 或 Log DataSource Bean, 但不能用于 dynamicDataSource(dataSource) Bean, 否则会产生循环调用
     * @ConfigurationProperties 注解用于从 application.properties 文件中读取配置，为 Bean 设置属性
     * @return data source
     */
    @Bean
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource.druid.core")
    public DataSource dataSourceCore(){
        return DruidDataSourceBuilder.create().build();
    }
    //	所有的核心库共享一个日志中心模块，改模块不采用mysql中的innodb引擎，采用归档引擎
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.druid.log")
    public DataSource dataSourceLog(){
        return DruidDataSourceBuilder.create().build();
    }

    @Bean // 只需要纳入动态数据源到spring容器
    public DataSource dataSource() {
        DynamicDataSource dataSource = new DynamicDataSource();
        DataSource coreDataSource =  dataSourceCore() ;
        DataSource logDataSource =  dataSourceLog();
        dataSource.addDataSource(DataSourceKey.core, coreDataSource);
        dataSource.addDataSource(DataSourceKey.log, logDataSource);
        dataSource.setDefaultTargetDataSource(coreDataSource);
        return dataSource;
    }

    private String dbParamterPrefix = "";
    public String getDbParamterPrefix() {
        return dbParamterPrefix;
    }
    public void setDbParamterPrefix(String dbParamterPrefix) {
        this.dbParamterPrefix = dbParamterPrefix;
    }

    @Bean(name="sqlHelper")
    public SqlHelper sqlHelper(@Autowired DataSource dataSource) {
        SqlHelper sqlHelper = new SqlHelper();
        sqlHelper.setDataSource(dataSource);
        if(dbParamterPrefix != null && dbParamterPrefix.trim().length() > 0){
            sqlHelper.setDbParamterPrefix(dbParamterPrefix);
        }
        System.out.println(sqlHelper.getDbParamterPrefix());
        return sqlHelper;
    }

    @Bean // 将数据源纳入spring事物管理
    public DataSourceTransactionManager transactionManager(@Qualifier("dataSource")  DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

}

